package com.bee.web.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bee.web.mapper.UserEntityMapper;
import com.bee.web.util.DateUtil;
import com.bee.web.util.IDUtil;
import com.bee.web.util.Page;
import com.bee.web.util.StringUtil;
import com.bee.web.vo.bean.SalerInfoBean;
import com.hengyuan.db.UserEntity;

@Service
public class SalerService {
	
	@Autowired
	private UserEntityMapper userEntityMapper;
    
    public int searchCount(SalerInfoBean bean, Page page) {
    	UserEntity entity = new UserEntity();
    	entity.setUserName(bean.getUserName());
    	entity.setPhoneNumber(bean.getPhoneNumber());
    	return userEntityMapper.searchCount(entity, page);
    }

    public List<SalerInfoBean> searchList(SalerInfoBean bean, Page page) throws Exception {
    	UserEntity entity = new UserEntity();
    	entity.setUserName(bean.getUserName());
    	entity.setPhoneNumber(bean.getPhoneNumber());
    	List<UserEntity> list = userEntityMapper.searchList(entity, page);
        List<SalerInfoBean> beans = new ArrayList<>();
        for (UserEntity e : list) {
            SalerInfoBean b = new SalerInfoBean();
            b.setUserId(e.getUserId());
            b.setUserName(e.getUserName());
            b.setNickName(e.getNickName());
            b.setPhoneNumber(e.getPhoneNumber());
            b.setStatus(e.getStatus());
            b.setCreateTime(DateUtil.format(e.getCreateTime()));
            b.setUpdateTime(DateUtil.format(e.getUpdateTime()));
            beans.add(b);
        }
        return beans;
    }

    public boolean switchStatus(String userId, String status) throws Exception {
    	UserEntity entity = new UserEntity();
    	entity.setUserId(userId);
        if ("Y".equals(status)) {
        	entity.setStatus("N");
        } else if ("N".equals(status)) {
        	entity.setStatus("Y");
        }
        if (userEntityMapper.update(entity) > 0) {
			return true;
		} else {
			return false;
		}
    }

	public boolean delete(String userId) throws Exception {
		if (userEntityMapper.delete(userId) > 0) {
			return true;
		} else {
			return false;
		}
	}

	public boolean saveOrUpdate(SalerInfoBean bean) {
		UserEntity e = new UserEntity();
		e.setUserId(bean.getUserId());
		e.setUserName(bean.getUserName());
		e.setNickName(bean.getNickName());
		e.setPhoneNumber(bean.getPhoneNumber());
		e.setStatus(bean.getStatus());
		int count = 0;
		if (StringUtil.isNullOrEmpty(bean.getUserId())) {
			e.setUserId(IDUtil.rand());
			e.setPassword("96e79218965eb72c92a549dd5a330112");
			e.setUserType("S");
			e.setStatus("Y");
			count = userEntityMapper.insert(e);
		} else {
			count = userEntityMapper.update(e);
		}
		if (count > 0) {
			return true;
		}
		return false;
	}

	public boolean isExists(String userId) {
		UserEntity entity = userEntityMapper.selectById(userId);
		if (entity == null) {
			return false;
		} else {
			return true;
		}
	}

}
